package sz.szsmk.citizencard.ui.fragment;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusCollecLineViewBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.mvp.contract.ICollecLineContact;
import sz.szsmk.citizencard.mvp.presenter.CollecLinePresenter;
import sz.szsmk.citizencard.ui.adapter.BusNearbyLineAdapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/15 10:22
 * description:
 */
public class BusCollecLineFragment extends IBaseFragment<CollecLinePresenter, BusCollecLineViewBinding>
        implements ICollecLineContact.ICollecLineView {

    private BusNearbyLineAdapter busNearbyLineAdapter;
    private List<QueryBusLineNearbyRes.BusLineArrayListBean> busLineArrayList = new ArrayList<>();

    public static BusCollecLineFragment getInstance() {
        BusCollecLineFragment sf = new BusCollecLineFragment();
        return sf;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_collec_line_view;
    }

    @Override
    protected void initInjector() {
        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        busNearbyLineAdapter = new BusNearbyLineAdapter(getActivity());

        dataBinding.collecLineRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        dataBinding.collecLineRv.setAdapter(busNearbyLineAdapter);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.queryCollectedBusLine("", "");
            }
        });


    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onResume() {
        super.onResume();
        dataBinding.swipeRefreshLayout.autoRefresh();
    }

    @Override
    public void queryCollectedBusLineFail(QueryBusLineNearbyRes response) {
        dataBinding.swipeRefreshLayout.finishRefresh();
    }

    @Override
    public void queryCollectedBusLineSucc(QueryBusLineNearbyRes response) {
        dataBinding.swipeRefreshLayout.finishRefresh();
        busLineArrayList.clear();
        busLineArrayList.addAll(response.getBusLineArrayList());
        busNearbyLineAdapter.setViewData(busLineArrayList);
        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

}
